package com.cogent.business.action;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.struts.ActionSupport;

import com.cogent.business.domain.UserDomain;
import com.cogent.business.helper.UserDomainHelper;
import com.cogent.business.session.SessionUtil;
import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.ConstantUtil;
import com.cogent.common.util.FormUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.facade.BussinessDaoFacade;

public class AbstractAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	protected BussinessDaoFacade bussinessFacade;
		
	public BussinessDaoFacade getBussinessFacade() {
		return bussinessFacade;
	}

	public void setBussinessFacade(BussinessDaoFacade bussinessFacade) {
		this.bussinessFacade = bussinessFacade;
	}

	public String execute() {
		return "";
	}
	
	public HashMap getFormMapNoUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HashMap curMap = FormUtil.getFormInfoNew(request);
				
		return curMap;
	}
	
	public HashMap getFormMap() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HashMap curMap = FormUtil.getFormInfoNew(request);
		UserDomain userDomain = UserDomainHelper.getUser(ServletActionContext.getRequest());
		if(userDomain != null)
			curMap.put(ConstantUtil.GLOBALS_USER_ID_KEY, userDomain.getId());
				
		return curMap;
	}
	
	public HashMap getFormMapWithUser() {
		HashMap curMap = getFormMap();
		UserDomain userDomain = UserDomainHelper.getUser(ServletActionContext.getRequest());
		if(userDomain != null) {
			if(MapUtil.isEmpty(curMap, "id")) {
				curMap.put("creator", userDomain.getName());
				curMap.put("createdate", JodaDateUtil.getCurDate());
				curMap.put("modifier", userDomain.getName());
				curMap.put("modifydate", JodaDateUtil.getCurDate());
			} else {
				curMap.put("modifier", userDomain.getName());
				curMap.put("modifyDate", JodaDateUtil.getCurDate());
			}
		 	 curMap.put(ConstantUtil.GLOBALS_USER_ID_KEY, userDomain.getId());
		} else {
			curMap.put("createdate", JodaDateUtil.getCurDate());
			curMap.put("modifydate", JodaDateUtil.getCurDate());
		}
				
		return curMap;
	}
	
	public UserDomain getUserInfo() {
		UserDomain userDomain = UserDomainHelper.getUser(ServletActionContext.getRequest());
		
		return userDomain;
	}
	
	public void setResponseMsg(String key, Object value) {
		ServletActionContext.getRequest().setAttribute(key, value);
	}
	
	public void setSessionInfo(String key, Object value) {
		SessionUtil.setRequestMsg(ServletActionContext.getRequest(), key, value);
	}	
	
	public void setNoteMsg(String formName, String notemsg) {
		SessionUtil.setNoteMsg(ServletActionContext.getRequest(), formName, notemsg);
	}	
	
	public void setResponseUrl(HashMap formMap, String url) {
		if(!MapUtil.isEmpty(formMap, "resourcecode")) {
			if(url.indexOf("?") > 0)
				url += "&";
			else
				url += "?";
			url += "resourcecode=" + MapUtil.getStringValue(formMap, "resourcecode");
		}
		if(!MapUtil.isEmpty(formMap, "type")) {
			if(url.indexOf("?") > 0)
				url += "&";
			else
				url += "?";
			url += "type=" + MapUtil.getStringValue(formMap, "type");
		}
		setResponseUrl(url);
	}
	
	public void setResponseUrl(String url) {
		try {
			ServletActionContext.getResponse().sendRedirect(url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected boolean isIdNull(HashMap dataInfo) {
		if(MapUtil.isEmpty(dataInfo, "id") && MapUtil.isEmpty(dataInfo, "uuid"))
			return true;
		return false;			
	}
	
	protected void responseMsg(String msg) {
		HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Charset", "utf-8");
        response.setHeader("Cache-Control", "no-cache");
        try {
			response.getWriter().println(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
 }